﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using QuantumWorkspace.SessionManager;

namespace OpenLS.Spreadsheet.UI.ViewModels
{
    public class LoginViewModel : INotifyPropertyChanged, IDataErrorInfo
    {
        private string _userName;
        private string _password;
        private bool _isPasswordForgotten;
        private string _error;

        private Dictionary<string, bool> validProperties;
        private bool allPropertiesValid = false;

        public bool AllPropertiesValid
        {
            get { return allPropertiesValid; }
            set
            {
                if (allPropertiesValid != value)
                {
                    allPropertiesValid = value;
                    // base.OnPropertyChanged("AllPropertiesValid");
                }
            }
        }

        #region ACTIONS METHODS

        /// <summary>
        /// Login action
        /// </summary>
        public bool DoLogin()
        {
            if (this.IsPasswordForgotten)
            {
                if (StaticAuthenticationDAL.Instance.SendUserPasswordRequest(this.UserName))
                {
                    this.Error = Internals.Resources.UIStringTable.PasswordSent;
                    return false;
                }
                else
                {
                    this.Error = Internals.Resources.UIStringTable.NoEmailProvided;
                    return false;
                }
            }
            if (StaticAuthenticationDAL.Instance.LogIn(this.UserName, this.Password))
            {
                return true;
            }
            else
            {
                if (StaticAuthenticationDAL.Instance.WrongPasswordForUser(this.UserName, this.Password))
                {
                    this.Error = Internals.Resources.UIStringTable.WrongPassword;
                }
                else
                {
                    this.Error = Internals.Resources.UIStringTable.UserDoesNotExist; 
                }
                
            }
            return false;
        }

        #endregion

        public string UserName
        {
            get
            {
                if (_userName == null)
                    _userName = "";
                return _userName;
            }
            set
            {
                _userName = value;
                notifyPropertyChanged("UserName");
            }
        }

        public string Password
        {
            get
            {
                if (_password == null)
                    _password = "";
                return _password;
            }
            set
            {
                _password = value;
                notifyPropertyChanged("Password");
            }
        }

        public bool IsPasswordForgotten
        {
            get { return _isPasswordForgotten; }
            set
            {
                _isPasswordForgotten = value;
                notifyPropertyChanged("IsPasswordForgotten");
            }
        }

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;

        private void notifyPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion

        public string this[string propertyName]
        {
            get
            {
                if(propertyName=="UserName")
                {
                    var regexItem = new Regex("[a-zA-Z0-9 ]*$");


                    if ( (UserName.Length>30 || UserName.Length<5) || (!regexItem.IsMatch(UserName)))
                    {
                        return Internals.Resources.UIStringTable.InvalidUserName;
                    }
                }

                if (propertyName == "Password")
                {
                    if (Password.Length < 6)
                        return Internals.Resources.UIStringTable.InvalidPasswordCreation;

                }

                return null;
            }
        }

        public string Error
        {
            get { return _error; }
            set 
            { 
                _error = value;
                notifyPropertyChanged("Error");
            }
        }
    }
}
